package com.bajiao.app.controller.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.constant.CommonConstant;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.menu.ResultEnum;
import com.bajiao.common.util.vans.RegexTool;
import com.bajiao.entity.user.User;
import com.bajiao.service.user.UserService;
import com.bajiao.third.utils.AliSmsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: FanJq
 * @date: 2024-10-10 16:30
 * @description: 短信控制层
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/sms")
@Api(tags = {"短信接口"})
public class RestSmsController extends AppBaseController {

    @Resource
    private UserService userService;

    @PostMapping(value = "/aliSend")
    @ApiOperation(value = "使用阿里云发送验证码", tags = {"短信接口"}, notes = "使用阿里云发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String"),
            @ApiImplicitParam(name = "event",required = true, dataType = "String",
                    value = "短信登录:login 注册:register 忘记密码:forget 信息变更:change(mobile不用传) 验证手机号码：verifyPhone"),
    })
    public RestResult<?> aliSend(@ModelAttribute("user") User user,
                                 @RequestParam(required = false) String mobile,
                                 @RequestParam String event,
                                 HttpServletRequest request) {

        if (StrUtil.hasBlank(mobile,event))
            return fail(ResultEnum.PARAMETER_MISSING);

        if (StrUtil.equals(event, CommonConstant.SMS_EVENT_REGISTER)) {
            //@create by FanJq 2024/12/25 15:17 注册
            user = userService.queryByMobileAndNotDelFlag(mobile);
            if (ObjectUtil.isNotNull(user))
                return fail(ResultEnum.MOBILE_EXIST_REGISTER);
        }
        if (StrUtil.equals(event, CommonConstant.SMS_EVENT_CHANGE)) {
            //@create by FanJq 2024/12/25 18:09 信息变更时 需验证token
            if (ObjectUtil.isNull(user)) return tokenOverdue();
            mobile = user.getMobile();
        }
        Integer code = (int) ((Math.random() * 9 + 1) * 100000);
        try {
            AliSmsUtils.sendCodeMessage(event, mobile, code, request);
        } catch (DataHandleException e) {
            return new RestResult(e.getCode(), e.getMessage(), null);
        }
        return success();
    }

    @PostMapping(value = "/devSend")
    @ApiOperation(value = "开发环境发送验证码", tags = {"短信接口"}, notes = "开发环境发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String"),
            @ApiImplicitParam(name = "event",required = true, dataType = "String",
                    value = "短信登录:login 注册:register 忘记密码:forget 信息变更:change(mobile不用传) 验证手机号码：verifyPhone"),
    })
    public RestResult<?> devSend(@ModelAttribute("user") User user,
                                 @RequestParam(required = false) String mobile,
                                 @RequestParam String event,
                                 HttpServletRequest request) {

        if (StrUtil.hasBlank(mobile,event))
            return fail(ResultEnum.PARAMETER_MISSING);
        if (!RegexTool.mobileCheck(mobile))
            throw new DataHandleException("手机号格式错误");

        if (StrUtil.equals(event, CommonConstant.SMS_EVENT_REGISTER)) {
            //@create by FanJq 2024/12/25 15:17 注册
            user = userService.queryByMobileAndNotDelFlag(mobile);
            if (ObjectUtil.isNotNull(user))
                return fail(ResultEnum.MOBILE_EXIST_REGISTER);
        }
        if (StrUtil.equals(event, CommonConstant.SMS_EVENT_CHANGE)) {
            //@create by FanJq 2024/12/25 18:09 信息变更时 需验证token
            if (ObjectUtil.isNull(user)) return tokenOverdue();
            mobile = user.getMobile();
        }
        Integer code = (int) ((Math.random() * 9 + 1) * 100000);
        try {
            AliSmsUtils.devSendCodeMessage(event, mobile, code, request);
        } catch (DataHandleException e) {
            return new RestResult(e.getCode(), e.getMessage(), null);
        }
        return success("", code);
    }

}
